

* TRAITS (unweighted)

set more off

*SWEDEN
*Intelligent and reliable are reversed so that higher values indicate more dislike/prejudice
foreach x of varlist Q6s1_1_SE Q6s2_1_SE Q6s3_1_SE Q6s4_1_SE Q6s5_1_SE Q6s6_1_SE Q6s7_1_SE Q6s8_1_SE {
local a= `a'+ 1
gen intelligent`a'_se=7-`x' 
}

foreach x of varlist Q6s1_2_SE Q6s2_2_SE Q6s3_2_SE Q6s4_2_SE Q6s5_2_SE Q6s6_2_SE Q6s7_2_SE Q6s8_2_SE {
local b= `b'+ 1
gen reliable`b'_se=7-`x' 
}

foreach x of varlist Q6s1_3_SE Q6s2_3_SE Q6s3_3_SE Q6s4_3_SE Q6s5_3_SE Q6s6_3_SE Q6s7_3_SE Q6s8_3_SE {
local c= `c'+ 1
gen selfish`c'_se=`x' 
}

foreach x of varlist Q6s1_4_SE Q6s2_4_SE Q6s3_4_SE Q6s4_4_SE Q6s5_4_SE Q6s6_4_SE Q6s7_4_SE Q6s8_4_SE {
local d= `d'+ 1
gen mean`d'_se=`x' 
}

*CREATE INTELLIGENT INDEX
*take the mean feeling towards all parties
egen intelligent_mean_se=rowmean(intelligent1_se-intelligent8_se)

*takes the difference between each party feeling from the mean
foreach x of varlist intelligent1_se-intelligent8_se {
bysort caseid: gen `x'_diff=(`x'-intelligent_mean_se)^2
}

*sum it up
egen intelligent_sum_se=rowtotal(intelligent1_se_diff-intelligent8_se_diff), missing

*divide by number of parties and take the square root
gen intelligent_sum_mean_se=(intelligent_sum_se/8)
		
gen intelligent_sq_se=sqrt(intelligent_sum_mean_se)

*normalize
gen intelligent_se=(intelligent_sq_se-0)/(2.5-0)


*CREATE REALIABLE INDEX
*take the mean feeling towards all parties
egen reliable_mean_se=rowmean(reliable1_se-reliable8_se)

*takes the difference between each party feeling from the mean
foreach x of varlist reliable1_se-reliable8_se {
bysort caseid: gen `x'_diff=(`x'-reliable_mean_se)^2
}

*sum it up
egen reliable_sum_se=rowtotal(reliable1_se_diff-reliable8_se_diff), missing

*divide by number of parties and take the square root
gen reliable_sum_mean_se=(reliable_sum_se/8)
		
gen reliable_sq_se=sqrt(reliable_sum_mean_se)

*normalize
gen reliable_se=(reliable_sq_se-0)/(2.5-0)


*CREATE SELFISH INDEX
*take the mean feeling towards all parties
egen selfish_mean_se=rowmean(selfish1_se-selfish8_se)

*takes the difference between each party feeling from the mean
foreach x of varlist selfish1_se-selfish8_se {
bysort caseid: gen `x'_diff=(`x'-selfish_mean_se)^2
}

*sum it up
egen selfish_sum_se=rowtotal(selfish1_se_diff-selfish8_se_diff), missing

*divide by number of parties and take the square root
gen selfish_sum_mean_se=(selfish_sum_se/8)
		
gen selfish_sq_se=sqrt(selfish_sum_mean_se)

*normalize
gen selfish_se=(selfish_sq_se-0)/(2.5-0)

*CREATE MEAN INDEX
*take the mean feeling towards all parties
egen mean_mean_se=rowmean(mean1_se-mean8_se)

*takes the difference between each party feeling from the mean
foreach x of varlist mean1_se-mean8_se {
bysort caseid: gen `x'_diff=(`x'-mean_mean_se)^2
}

*sum it up
egen mean_sum_se=rowtotal(mean1_se_diff-mean8_se_diff), missing

*divide by number of parties and take the square root
gen mean_sum_mean_se=(mean_sum_se/8)
		
gen mean_sq_se=sqrt(mean_sum_mean_se)

*normalize
gen mean_se=(mean_sq_se-0)/(2.5-0)

	
*TRAITS INDEX UNWEIGHTED
factor intelligent_se reliable_se selfish_se mean_se
rotate
*one factor
alpha intelligent_se reliable_se selfish_se mean_se, item
*0.92 in full sample
egen traits_se=rowmean(intelligent_se reliable_se selfish_se mean_se)



****************************************

*TRAITS WEIGHTED



*****intelligent
			bysort caseid: gen intelligent1_se_w=(intelligent1_se*0.3033)
			bysort caseid: gen intelligent2_se_w=(intelligent2_se*0.0675)
			bysort caseid: gen intelligent3_se_w=(intelligent3_se*0.0508)
			bysort caseid: gen intelligent4_se_w=(intelligent4_se*0.0671) 
			bysort caseid: gen intelligent5_se_w=(intelligent5_se*0.0461)
			bysort caseid: gen intelligent6_se_w=(intelligent6_se*0.1910)
			bysort caseid: gen intelligent7_se_w=(intelligent7_se*0.0534)
			bysort caseid: gen intelligent8_se_w=(intelligent8_se*0.2054) 
		
											
	**then sum all of sympathy_p1_v sympathy_p2_v for each year cluster to get the average like-i etc...
			egen intelligent_sum_w_se=rowtotal(intelligent1_se_w intelligent2_se_w intelligent3_se_w intelligent4_se_w intelligent5_se_w intelligent6_se_w intelligent7_se_w intelligent8_se_w), missing
			
			
	*now i want to subtract the weighted intelligent of sympathies from the sympathy for each individual party.
	*then square it 
			
			bysort caseid: gen intelligent1_se_w_se=0.3033*(intelligent1_se-intelligent_sum_w_se)
			bysort caseid: gen intelligent2_se_w_se=0.0675*(intelligent2_se-intelligent_sum_w_se)
			bysort caseid: gen intelligent3_se_w_se=0.0508*(intelligent3_se-intelligent_sum_w_se)
			bysort caseid: gen intelligent4_se_w_se=0.0671*(intelligent4_se-intelligent_sum_w_se)
			bysort caseid: gen intelligent5_se_w_se=0.0461*(intelligent5_se-intelligent_sum_w_se)
			bysort caseid: gen intelligent6_se_w_se=0.1910*(intelligent6_se-intelligent_sum_w_se)
			bysort caseid: gen intelligent7_se_w_se=0.0534*(intelligent7_se-intelligent_sum_w_se)
			bysort caseid: gen intelligent8_se_w_se=0.2054*(intelligent8_se-intelligent_sum_w_se)
		
		
			bysort caseid: gen intelligent1_se_w_se_sq=intelligent1_se_w_se^2
			bysort caseid: gen intelligent2_se_w_se_sq=intelligent2_se_w_se^2
			bysort caseid: gen intelligent3_se_w_se_sq=intelligent3_se_w_se^2
			bysort caseid: gen intelligent4_se_w_se_sq=intelligent4_se_w_se^2
			bysort caseid: gen intelligent5_se_w_se_sq=intelligent5_se_w_se^2
			bysort caseid: gen intelligent6_se_w_se_sq=intelligent6_se_w_se^2
			bysort caseid: gen intelligent7_se_w_se_sq=intelligent7_se_w_se^2
			bysort caseid: gen intelligent8_se_w_se_sq=intelligent8_se_w_se^2
	*then sum it all together  
									
			egen intelligent_tot_sum_se=rowtotal(intelligent1_se_w_se_sq-intelligent8_se_w_se_sq), missing
			
	*lastly take the square root as Harteveld does
			
		bysort caseid: gen intelligent_sqrt_se=sqrt(intelligent_tot_sum_se)									
											
	*normalized variable
	gen intelligent_w_n_se=(intelligent_sqrt_se- .0066645 )/(1.14029-.0066645 )	
	
	

*****reliable
			bysort caseid: gen reliable1_se_w=(reliable1_se*0.3033)
			bysort caseid: gen reliable2_se_w=(reliable2_se*0.0675)
			bysort caseid: gen reliable3_se_w=(reliable3_se*0.0508)
			bysort caseid: gen reliable4_se_w=(reliable4_se*0.0671) 
			bysort caseid: gen reliable5_se_w=(reliable5_se*0.0461)
			bysort caseid: gen reliable6_se_w=(reliable6_se*0.1910)
			bysort caseid: gen reliable7_se_w=(reliable7_se*0.0534)
			bysort caseid: gen reliable8_se_w=(reliable8_se*0.2054) 
		
											
	**then sum all of sympathy_p1_v sympathy_p2_v for each year cluster to get the average like-i etc...
			egen reliable_sum_w_se=rowtotal(reliable1_se_w reliable2_se_w reliable3_se_w reliable4_se_w reliable5_se_w reliable6_se_w reliable7_se_w reliable8_se_w), missing
			
			
	*now i want to subtract the weighted reliable of sympathies from the sympathy for each individual party.
	*then square it 
			bysort caseid: gen reliable1_se_w_se=0.3033*(reliable1_se-reliable_sum_w_se)
			bysort caseid: gen reliable2_se_w_se=0.0675*(reliable2_se-reliable_sum_w_se)
			bysort caseid: gen reliable3_se_w_se=0.0508*(reliable3_se-reliable_sum_w_se)
			bysort caseid: gen reliable4_se_w_se=0.0671*(reliable4_se-reliable_sum_w_se)
			bysort caseid: gen reliable5_se_w_se=0.0461*(reliable5_se-reliable_sum_w_se)
			bysort caseid: gen reliable6_se_w_se=0.1910*(reliable6_se-reliable_sum_w_se)
			bysort caseid: gen reliable7_se_w_se=0.0534*(reliable7_se-reliable_sum_w_se)
			bysort caseid: gen reliable8_se_w_se=0.2054*(reliable8_se-reliable_sum_w_se)
		
		
			bysort caseid: gen reliable1_se_w_se_sq=reliable1_se_w_se^2
			bysort caseid: gen reliable2_se_w_se_sq=reliable2_se_w_se^2
			bysort caseid: gen reliable3_se_w_se_sq=reliable3_se_w_se^2
			bysort caseid: gen reliable4_se_w_se_sq=reliable4_se_w_se^2
			bysort caseid: gen reliable5_se_w_se_sq=reliable5_se_w_se^2
			bysort caseid: gen reliable6_se_w_se_sq=reliable6_se_w_se^2
			bysort caseid: gen reliable7_se_w_se_sq=reliable7_se_w_se^2
			bysort caseid: gen reliable8_se_w_se_sq=reliable8_se_w_se^2
		
			
	*then sum it all together  
									
			egen reliable_tot_sum_se=rowtotal(reliable1_se_w_se_sq-reliable8_se_w_se_sq), missing
			
	*lastly take the square root as Harteveld does
			
		bysort caseid: gen reliable_sqrt_se=sqrt(reliable_tot_sum_se)									
											
	*normalized variable
	gen reliable_w_n_se=(reliable_sqrt_se-.0066645)/(1.123501-.0066645 )
	
	
*****selfish
			bysort caseid: gen selfish1_se_w=(selfish1_se*0.3033)
			bysort caseid: gen selfish2_se_w=(selfish2_se*0.0675)
			bysort caseid: gen selfish3_se_w=(selfish3_se*0.0508)
			bysort caseid: gen selfish4_se_w=(selfish4_se*0.0671) 
			bysort caseid: gen selfish5_se_w=(selfish5_se*0.0461)
			bysort caseid: gen selfish6_se_w=(selfish6_se*0.1910)
			bysort caseid: gen selfish7_se_w=(selfish7_se*0.0534)
			bysort caseid: gen selfish8_se_w=(selfish8_se*0.2054) 
		
											
	**then sum all of sympathy_p1_v sympathy_p2_v for each year cluster to get the average like-i etc...
			egen selfish_sum_w_se=rowtotal(selfish1_se_w selfish2_se_w selfish3_se_w selfish4_se_w selfish5_se_w selfish6_se_w selfish7_se_w selfish8_se_w), missing
			
			
	*now i want to subtract the weighted selfish of sympathies from the sympathy for each individual party.
	*then square it 
			bysort caseid: gen selfish1_se_w_se=0.3033*(selfish1_se-selfish_sum_w_se)
			bysort caseid: gen selfish2_se_w_se=0.0675*(selfish2_se-selfish_sum_w_se)
			bysort caseid: gen selfish3_se_w_se=0.0508*(selfish3_se-selfish_sum_w_se)
			bysort caseid: gen selfish4_se_w_se=0.0671*(selfish4_se-selfish_sum_w_se)
			bysort caseid: gen selfish5_se_w_se=0.0461*(selfish5_se-selfish_sum_w_se)
			bysort caseid: gen selfish6_se_w_se=0.1910*(selfish6_se-selfish_sum_w_se)
			bysort caseid: gen selfish7_se_w_se=0.0534*(selfish7_se-selfish_sum_w_se)
			bysort caseid: gen selfish8_se_w_se=0.2054*(selfish8_se-selfish_sum_w_se)
		
		
			bysort caseid: gen selfish1_se_w_se_sq=selfish1_se_w_se^2
			bysort caseid: gen selfish2_se_w_se_sq=selfish2_se_w_se^2
			bysort caseid: gen selfish3_se_w_se_sq=selfish3_se_w_se^2
			bysort caseid: gen selfish4_se_w_se_sq=selfish4_se_w_se^2
			bysort caseid: gen selfish5_se_w_se_sq=selfish5_se_w_se^2
			bysort caseid: gen selfish6_se_w_se_sq=selfish6_se_w_se^2
			bysort caseid: gen selfish7_se_w_se_sq=selfish7_se_w_se^2
			bysort caseid: gen selfish8_se_w_se_sq=selfish8_se_w_se^2
		
			
	*then sum it all together  
									
			egen selfish_tot_sum_se=rowtotal(selfish1_se_w_se_sq-selfish8_se_w_se_sq), missing
			
	*lastly take the square root as Harteveld does
			
		bysort caseid: gen selfish_sqrt_se=sqrt(selfish_tot_sum_se)									
											
	*normalized variable
	gen selfish_w_n_se=(selfish_sqrt_se-.0066645 )/(1.143618-.0066645    )							
	

*****mean
			bysort caseid: gen mean1_se_w=(mean1_se*0.3033)
			bysort caseid: gen mean2_se_w=(mean2_se*0.0675)
			bysort caseid: gen mean3_se_w=(mean3_se*0.0508)
			bysort caseid: gen mean4_se_w=(mean4_se*0.0671) 
			bysort caseid: gen mean5_se_w=(mean5_se*0.0461)
			bysort caseid: gen mean6_se_w=(mean6_se*0.1910)
			bysort caseid: gen mean7_se_w=(mean7_se*0.0534)
			bysort caseid: gen mean8_se_w=(mean8_se*0.2054) 
		
											
	**then sum all of sympathy_p1_v sympathy_p2_v for each year cluster to get the average like-i etc...
			egen mean_sum_w_se=rowtotal(mean1_se_w mean2_se_w mean3_se_w mean4_se_w mean5_se_w mean6_se_w mean7_se_w mean8_se_w), missing
			
			
	*now i want to subtract the weighted mean of sympathies from the sympathy for each individual party.
	*then square it 
			bysort caseid: gen mean1_se_w_se=0.3033*(mean1_se-mean_sum_w_se)
			bysort caseid: gen mean2_se_w_se=0.0675*(mean2_se-mean_sum_w_se)
			bysort caseid: gen mean3_se_w_se=0.0508*(mean3_se-mean_sum_w_se)
			bysort caseid: gen mean4_se_w_se=0.0671*(mean4_se-mean_sum_w_se)
			bysort caseid: gen mean5_se_w_se=0.0461*(mean5_se-mean_sum_w_se)
			bysort caseid: gen mean6_se_w_se=0.1910*(mean6_se-mean_sum_w_se)
			bysort caseid: gen mean7_se_w_se=0.0534*(mean7_se-mean_sum_w_se)
			bysort caseid: gen mean8_se_w_se=0.2054*(mean8_se-mean_sum_w_se)
		
		
			bysort caseid: gen mean1_se_w_se_sq=mean1_se_w_se^2
			bysort caseid: gen mean2_se_w_se_sq=mean2_se_w_se^2
			bysort caseid: gen mean3_se_w_se_sq=mean3_se_w_se^2
			bysort caseid: gen mean4_se_w_se_sq=mean4_se_w_se^2
			bysort caseid: gen mean5_se_w_se_sq=mean5_se_w_se^2
			bysort caseid: gen mean6_se_w_se_sq=mean6_se_w_se^2
			bysort caseid: gen mean7_se_w_se_sq=mean7_se_w_se^2
			bysort caseid: gen mean8_se_w_se_sq=mean8_se_w_se^2
	
		
			
	*then sum it all together  
									
			egen mean_tot_sum_se=rowtotal(mean1_se_w_se_sq-mean8_se_w_se_sq), missing
			
	*lastly take the square root as Harteveld does
			
		bysort caseid: gen mean_sqrt_se=sqrt(mean_tot_sum_se)									
											
	*normalized variable
	gen mean_w_n_se=(mean_sqrt_se- .0066645  )/(1.142151-.0066645 )
	


*TRAITS INDEX WEIGHTED
factor intelligent_w_n_se reliable_w_n_se selfish_w_n_se mean_w_n_se
rotate
*two factors (int+reli, and self+mean)
alpha  intelligent_w_n_se reliable_w_n_se selfish_w_n_se mean_w_n_se, item
*0.6466 in full sample
egen traits_se_w=rowmean(intelligent_w_n_se reliable_w_n_se selfish_w_n_se mean_w_n_se)


**********************************************************************************

*UNWEIGHTED MEAN DISTANCE FROM MOST LIKED PARTY

*the value for the most liked party in any of the waves
egen intelligent_max_se=rowmax(intelligent1_se-intelligent8_se)

egen reliable_max_se=rowmax(reliable1_se-reliable8_se)

egen selfish_max_se=rowmax(selfish1_se-selfish8_se)

egen mean_max_se=rowmax(mean1_se-mean8_se)

*the mean of this
egen traits_max_mean_se=rowmean(intelligent_max_se reliable_max_se selfish_max_se mean_max_se)

*like_ip-like_max^2
	*take the difference between each party feeling from the mean
foreach x of varlist intelligent1_se-intelligent8_se {
bysort caseid: gen `x'_diff_max=(`x'-intelligent_max_se)^2
}

*takes the difference between each party feeling from the mean
foreach x of varlist reliable1_se-reliable8_se {
bysort caseid: gen `x'_diff_max=(`x'-reliable_max_se)^2
}

foreach x of varlist selfish1_se-selfish8_se {
bysort caseid: gen `x'_diff_max=(`x'-selfish_max_se)^2
}

foreach x of varlist mean1_se-mean8_se {
bysort caseid: gen `x'_diff_max=(`x'-mean_max_se)^2
}

*sum it up
egen traits_max_sum_se=rowtotal(intelligent1_se_diff_max-intelligent8_se_diff_max reliable1_se_diff_max-reliable8_se_diff_max  selfish1_se_diff_max-selfish8_se_diff_max mean1_se_diff_max-mean8_se_diff_max)

*divide by number of parties and take the square root
	by caseid: gen traits_max_tot_se=sqrt(traits_max_sum_se/8)
													
	gen traits_dist_n_se=(traits_max_tot_se-0)/ (9.354143-0)
	*remove all Danes
	replace traits_dist_n_se=. if id_dk!=.
	
	
	
	******************************************************

*WEIGHTED MEAN DISTANCE FROM MOST LIKED PARTY

*multiply the vote share of each party by (like_ip-like_p_mean_max)^2 
			bysort caseid: gen intelligent1_max_w_se=0.3033*(intelligent1_se-traits_max_mean_se)
			bysort caseid: gen intelligent2_max_w_se=0.0675*(intelligent2_se-traits_max_mean_se)
			bysort caseid: gen intelligent3_max_w_se=0.0508*(intelligent3_se-traits_max_mean_se)
			bysort caseid: gen intelligent4_max_w_se=0.0671*(intelligent4_se-traits_max_mean_se)
			bysort caseid: gen intelligent5_max_w_se=0.0461*(intelligent5_se-traits_max_mean_se)
			bysort caseid: gen intelligent6_max_w_se=0.1910*(intelligent6_se-traits_max_mean_se)
			bysort caseid: gen intelligent7_max_w_se=0.0534*(intelligent7_se-traits_max_mean_se)
			bysort caseid: gen intelligent8_max_w_se=0.2054*(intelligent8_se-traits_max_mean_se)
			
			bysort caseid: gen intelligent1_max_w_sq_se=intelligent1_max_w_se^2
			bysort caseid: gen intelligent2_max_w_sq_se=intelligent2_max_w_se^2
			bysort caseid: gen intelligent3_max_w_sq_se=intelligent3_max_w_se^2
			bysort caseid: gen intelligent4_max_w_sq_se=intelligent4_max_w_se^2
			bysort caseid: gen intelligent5_max_w_sq_se=intelligent5_max_w_se^2
			bysort caseid: gen intelligent6_max_w_sq_se=intelligent6_max_w_se^2
			bysort caseid: gen intelligent7_max_w_sq_se=intelligent7_max_w_se^2
			bysort caseid: gen intelligent8_max_w_sq_se=intelligent8_max_w_se^2

			bysort caseid: gen reliable1_max_w_se=0.3033*(reliable1_se-traits_max_mean_se)
			bysort caseid: gen reliable2_max_w_se=0.0675*(reliable2_se-traits_max_mean_se)
			bysort caseid: gen reliable3_max_w_se=0.0508*(reliable3_se-traits_max_mean_se)
			bysort caseid: gen reliable4_max_w_se=0.0671*(reliable4_se-traits_max_mean_se)
			bysort caseid: gen reliable5_max_w_se=0.0461*(reliable5_se-traits_max_mean_se)
			bysort caseid: gen reliable6_max_w_se=0.1910*(reliable6_se-traits_max_mean_se)
			bysort caseid: gen reliable7_max_w_se=0.0534*(reliable7_se-traits_max_mean_se)
			bysort caseid: gen reliable8_max_w_se=0.2054*(reliable8_se-traits_max_mean_se)
			
			bysort caseid: gen reliable1_max_w_sq_se=reliable1_max_w_se^2
			bysort caseid: gen reliable2_max_w_sq_se=reliable2_max_w_se^2
			bysort caseid: gen reliable3_max_w_sq_se=reliable3_max_w_se^2
			bysort caseid: gen reliable4_max_w_sq_se=reliable4_max_w_se^2
			bysort caseid: gen reliable5_max_w_sq_se=reliable5_max_w_se^2
			bysort caseid: gen reliable6_max_w_sq_se=reliable6_max_w_se^2
			bysort caseid: gen reliable7_max_w_sq_se=reliable7_max_w_se^2
			bysort caseid: gen reliable8_max_w_sq_se=reliable8_max_w_se^2
			
			
			bysort caseid: gen selfish1_max_w_se=0.3033*(selfish1_se-traits_max_mean_se)
			bysort caseid: gen selfish2_max_w_se=0.0675*(selfish2_se-traits_max_mean_se)
			bysort caseid: gen selfish3_max_w_se=0.0508*(selfish3_se-traits_max_mean_se)
			bysort caseid: gen selfish4_max_w_se=0.0671*(selfish4_se-traits_max_mean_se)
			bysort caseid: gen selfish5_max_w_se=0.0461*(selfish5_se-traits_max_mean_se)
			bysort caseid: gen selfish6_max_w_se=0.1910*(selfish6_se-traits_max_mean_se)
			bysort caseid: gen selfish7_max_w_se=0.0534*(selfish7_se-traits_max_mean_se)
			bysort caseid: gen selfish8_max_w_se=0.2054*(selfish8_se-traits_max_mean_se)
			
			bysort caseid: gen selfish1_max_w_sq_se=selfish1_max_w_se^2
			bysort caseid: gen selfish2_max_w_sq_se=selfish2_max_w_se^2
			bysort caseid: gen selfish3_max_w_sq_se=selfish3_max_w_se^2
			bysort caseid: gen selfish4_max_w_sq_se=selfish4_max_w_se^2
			bysort caseid: gen selfish5_max_w_sq_se=selfish5_max_w_se^2
			bysort caseid: gen selfish6_max_w_sq_se=selfish6_max_w_se^2
			bysort caseid: gen selfish7_max_w_sq_se=selfish7_max_w_se^2
			bysort caseid: gen selfish8_max_w_sq_se=selfish8_max_w_se^2
			
			
			bysort caseid: gen mean1_max_w_se=0.3033*(mean1_se-traits_max_mean_se)
			bysort caseid: gen mean2_max_w_se=0.0675*(mean2_se-traits_max_mean_se)
			bysort caseid: gen mean3_max_w_se=0.0508*(mean3_se-traits_max_mean_se)
			bysort caseid: gen mean4_max_w_se=0.0671*(mean4_se-traits_max_mean_se)
			bysort caseid: gen mean5_max_w_se=0.0461*(mean5_se-traits_max_mean_se)
			bysort caseid: gen mean6_max_w_se=0.1910*(mean6_se-traits_max_mean_se)
			bysort caseid: gen mean7_max_w_se=0.0534*(mean7_se-traits_max_mean_se)
			bysort caseid: gen mean8_max_w_se=0.2054*(mean8_se-traits_max_mean_se)
			
			bysort caseid: gen mean1_max_w_sq_se=mean1_max_w_se^2
			bysort caseid: gen mean2_max_w_sq_se=mean2_max_w_se^2
			bysort caseid: gen mean3_max_w_sq_se=mean3_max_w_se^2
			bysort caseid: gen mean4_max_w_sq_se=mean4_max_w_se^2
			bysort caseid: gen mean5_max_w_sq_se=mean5_max_w_se^2
			bysort caseid: gen mean6_max_w_sq_se=mean6_max_w_se^2
			bysort caseid: gen mean7_max_w_sq_se=mean7_max_w_se^2
			bysort caseid: gen mean8_max_w_sq_se=mean8_max_w_se^2
			
			
			*sum it up
			egen traits_sum_max_se=rowtotal(intelligent1_max_w_sq_se-intelligent8_max_w_sq_se reliable1_max_w_sq_se-reliable8_max_w_sq_se selfish1_max_w_sq_se-selfish8_max_w_sq_se mean1_max_w_sq_se-mean8_max_w_sq_se) 
			
			by caseid: gen traits_max_v_se=sqrt(traits_sum_max_se)
	
			gen traits_dist_n_se_w=(traits_max_v_se-0)/ (4.274586-0)


			
			

*------------------------------------------------------------------------------

*-----------------------------------------------------------------------------



*DENMARK
*intelligent and reliable reversed so that higher values indicate more dislike/prejudice
foreach x of varlist Q6d1_1_DK Q6d2_1_DK Q6d3_1_DK Q6d4_1_DK Q6d5_1_DK Q6d6_1_DK Q6d7_1_DK Q6d8_1_DK Q6d9_1_DK Q6d10_1_DK Q6d11_1_DK Q6d12_1_DK {
local e= `e'+ 1
gen intelligent`e'_DK=7-`x' 
}

foreach x of varlist Q6d1_2_DK Q6d2_2_DK Q6d3_2_DK Q6d4_2_DK Q6d5_2_DK Q6d6_2_DK Q6d7_2_DK Q6d8_2_DK Q6d9_2_DK Q6d10_2_DK Q6d11_2_DK Q6d12_2_DK {
local f= `f'+ 1
gen reliable`f'_DK=7-`x' 
}

foreach x of varlist Q6d1_3_DK Q6d2_3_DK Q6d3_3_DK Q6d4_3_DK Q6d5_3_DK Q6d6_3_DK Q6d7_3_DK Q6d8_3_DK Q6d9_3_DK Q6d10_3_DK Q6d11_3_DK Q6d12_3_DK {
local g= `g'+ 1
gen selfish`g'_DK=`x' 
}

foreach x of varlist Q6d1_4_DK Q6d2_4_DK Q6d3_4_DK Q6d4_4_DK Q6d5_4_DK Q6d6_4_DK Q6d7_4_DK Q6d8_4_DK Q6d9_4_DK Q6d10_4_DK Q6d11_4_DK Q6d12_4_DK {
local h= `h'+ 1
gen mean`h'_DK=`x' 
}

*CREATE INTELLIGENT INDEX
*take the mean feeling towards all parties
egen intelligent_mean_dk=rowmean(intelligent1_DK-intelligent12_DK)

*takes the difference between each party feeling from the mean
foreach x of varlist intelligent1_DK-intelligent12_DK {
bysort caseid: gen `x'_diff=(`x'-intelligent_mean_dk)^2
}

*sum it up
egen intelligent_sum_dk=rowtotal(intelligent1_DK_diff-intelligent12_DK_diff), missing

*divide by number of parties and take the square root
gen intelligent_sum_mean_dk=(intelligent_sum_dk/12)
		
gen intelligent_sq_dk=sqrt(intelligent_sum_mean_dk)

*normalize
gen intelligent_dk=(intelligent_sq_dk-0)/(2.430992-0)


*CREATE REALIABLE INDEX
*take the mean feeling towards all parties
egen reliable_mean_dk=rowmean(reliable1_DK-reliable12_DK)

*takes the difference between each party feeling from the mean
foreach x of varlist reliable1_DK-reliable12_DK {
bysort caseid: gen `x'_diff=(`x'-reliable_mean_dk)^2
}

*sum it up
egen reliable_sum_dk=rowtotal(reliable1_DK_diff-reliable12_DK_diff), missing

*divide by number of parties and take the square root
gen reliable_sum_mean_dk=(reliable_sum_dk/12)
		
gen reliable_sq_dk=sqrt(reliable_sum_mean_dk)

*normalize
gen reliable_dk=(reliable_sq_dk-0)/(2.465033-0)


*CREATE SELFISH INDEX
*take the mean feeling towards all parties
egen selfish_mean_dk=rowmean(selfish1_DK-selfish12_DK)

*takes the difference between each party feeling from the mean
foreach x of varlist selfish1_DK-selfish12_DK {
bysort caseid: gen `x'_diff=(`x'-selfish_mean_dk)^2
}

*sum it up
egen selfish_sum_dk=rowtotal(selfish1_DK_diff-selfish12_DK_diff), missing

*divide by number of parties and take the square root
gen selfish_sum_mean_dk=(selfish_sum_dk/12)
		
gen selfish_sq_dk=sqrt(selfish_sum_mean_dk)

*normalize
gen selfish_dk=(selfish_sq_dk-0)/(2.5-0)

*CREATE MEAN INDEX
*take the mean feeling towards all parties
egen mean_mean_dk=rowmean(mean1_DK-mean12_DK)

*takes the difference between each party feeling from the mean
foreach x of varlist mean1_DK-mean12_DK {
bysort caseid: gen `x'_diff=(`x'-mean_mean_dk)^2
}

*sum it up
egen mean_sum_dk=rowtotal(mean1_DK_diff-mean12_DK_diff), missing

*divide by number of parties and take the square root
gen mean_sum_mean_dk=(mean_sum_dk/12)
		
gen mean_sq_dk=sqrt(mean_sum_mean_dk)

*normalize
gen mean_dk=(mean_sq_dk-0)/(2.5-0)

	
*TRAITS INDEX UNWEIGHTED
factor intelligent_dk reliable_dk selfish_dk mean_dk
rotate
*one factor
alpha intelligent_dk reliable_dk selfish_dk mean_dk, item
*0.89 in full sample
egen traits_dk=rowmean(intelligent_dk reliable_dk selfish_dk mean_dk)

egen traits_n=rowtotal(traits_se traits_dk)

******************************************************************

 
*TRAITS (weighted)

	*****Intelligent
			bysort caseid: gen intelligent1_DK_w=(intelligent1_DK*0.2750)
			bysort caseid: gen intelligent2_DK_w=(intelligent2_DK*0.1331)
			bysort caseid: gen intelligent3_DK_w=(intelligent3_DK*0.0927)
			bysort caseid: gen intelligent4_DK_w=(intelligent4_DK*0.0830) 
			bysort caseid: gen intelligent5_DK_w=(intelligent5_DK*0.0812)
			bysort caseid: gen intelligent6_DK_w=(intelligent6_DK*0.0788)
			bysort caseid: gen intelligent7_DK_w=(intelligent7_DK*0.0551)
			bysort caseid: gen intelligent8_DK_w=(intelligent8_DK*0.0514) 
			bysort caseid: gen intelligent9_DK_w=(intelligent9_DK*0.0379)
			bysort caseid: gen intelligent10_DK_w=(intelligent10_DK*0.0264)
			bysort caseid: gen intelligent11_DK_w=(intelligent11_DK*0.0333)
			bysort caseid: gen intelligent12_DK_w=(intelligent12_DK*0.0367) 
											
	**then sum all of sympathy_p1_v sympathy_p2_v for each year cluster to get the average like-i etc...
			egen intelligent_sum_w_dk=rowtotal(intelligent1_DK_w intelligent2_DK_w intelligent3_DK_w intelligent4_DK_w intelligent5_DK_w intelligent6_DK_w intelligent7_DK_w intelligent8_DK_w intelligent9_DK_w intelligent10_DK_w intelligent11_DK_w intelligent12_DK_w), missing
			
			
	*now i want to subtract the weighted mean of sympathies from the sympathy for each individual party.
	*then square it 
			bysort caseid: gen intelligent1_DK_w_dk=0.2750*(intelligent1_DK-intelligent_sum_w_dk)
			bysort caseid: gen intelligent2_DK_w_dk=0.1331*(intelligent2_DK-intelligent_sum_w_dk)
			bysort caseid: gen intelligent3_DK_w_dk=0.0927*(intelligent3_DK-intelligent_sum_w_dk)
			bysort caseid: gen intelligent4_DK_w_dk=0.0830*(intelligent4_DK-intelligent_sum_w_dk)
			bysort caseid: gen intelligent5_DK_w_dk=0.0812*(intelligent5_DK-intelligent_sum_w_dk)
			bysort caseid: gen intelligent6_DK_w_dk=0.0788*(intelligent6_DK-intelligent_sum_w_dk)
			bysort caseid: gen intelligent7_DK_w_dk=0.0551*(intelligent7_DK-intelligent_sum_w_dk)
			bysort caseid: gen intelligent8_DK_w_dk=0.0514*(intelligent8_DK-intelligent_sum_w_dk)
			bysort caseid: gen intelligent9_DK_w_dk=0.0379*(intelligent9_DK-intelligent_sum_w_dk)
			bysort caseid: gen intelligent10_DK_w_dk=0.0264*(intelligent10_DK-intelligent_sum_w_dk)
			bysort caseid: gen intelligent11_DK_w_dk=0.0333*(intelligent11_DK-intelligent_sum_w_dk)
			bysort caseid: gen intelligent12_DK_w_dk=0.0367*(intelligent12_DK-intelligent_sum_w_dk)
		
		
			bysort caseid: gen intelligent1_DK_w_dk_sq=intelligent1_DK_w_dk^2
			bysort caseid: gen intelligent2_DK_w_dk_sq=intelligent2_DK_w_dk^2
			bysort caseid: gen intelligent3_DK_w_dk_sq=intelligent3_DK_w_dk^2
			bysort caseid: gen intelligent4_DK_w_dk_sq=intelligent4_DK_w_dk^2
			bysort caseid: gen intelligent5_DK_w_dk_sq=intelligent5_DK_w_dk^2
			bysort caseid: gen intelligent6_DK_w_dk_sq=intelligent6_DK_w_dk^2
			bysort caseid: gen intelligent7_DK_w_dk_sq=intelligent7_DK_w_dk^2
			bysort caseid: gen intelligent8_DK_w_dk_sq=intelligent8_DK_w_dk^2
			bysort caseid: gen intelligent9_DK_w_dk_sq=intelligent9_DK_w_dk^2
			bysort caseid: gen intelligent10_DK_w_dk_sq=intelligent10_DK_w_dk^2
			bysort caseid: gen intelligent11_DK_w_dk_sq=intelligent12_DK_w_dk^2
			bysort caseid: gen intelligent12_DK_w_dk_sq=intelligent12_DK_w_dk^2
		
			
	*then sum it all together  
									
			egen intelligent_tot_sum=rowtotal(intelligent1_DK_w_dk_sq-intelligent12_DK_w_dk_sq), missing
			
	*lastly take the square root as Harteveld does
			
			bysort caseid: gen intelligent_sqrt=sqrt(intelligent_tot_sum)									
											
	*normalized variable
	gen intelligent_w_n_dk=(intelligent_sqrt-.0055979  )/(.9463062-.0055979  )
	
	

*****reliable
			bysort caseid: gen reliable1_DK_w=(reliable1_DK*0.2750)
			bysort caseid: gen reliable2_DK_w=(reliable2_DK*0.1331)
			bysort caseid: gen reliable3_DK_w=(reliable3_DK*0.0927)
			bysort caseid: gen reliable4_DK_w=(reliable4_DK*0.0830) 
			bysort caseid: gen reliable5_DK_w=(reliable5_DK*0.0812)
			bysort caseid: gen reliable6_DK_w=(reliable6_DK*0.0788)
			bysort caseid: gen reliable7_DK_w=(reliable7_DK*0.0551)
			bysort caseid: gen reliable8_DK_w=(reliable8_DK*0.0514) 
			bysort caseid: gen reliable9_DK_w=(reliable9_DK*0.0379)
			bysort caseid: gen reliable10_DK_w=(reliable10_DK*0.0264)
			bysort caseid: gen reliable11_DK_w=(reliable11_DK*0.0333)
			bysort caseid: gen reliable12_DK_w=(reliable12_DK*0.0367) 
											
	**then sum all of sympathy_p1_v sympathy_p2_v for each year cluster to get the average like-i etc...
			egen reliable_sum_w_dk=rowtotal(reliable1_DK_w reliable2_DK_w reliable3_DK_w reliable4_DK_w reliable5_DK_w reliable6_DK_w reliable7_DK_w reliable8_DK_w reliable9_DK_w reliable10_DK_w reliable11_DK_w reliable12_DK_w), missing
			
			
	*now i want to subtract the weighted mean of sympathies from the sympathy for each individual party.
	*then square it 
			bysort caseid: gen reliable1_DK_w_dk=0.2750*(reliable1_DK-reliable_sum_w_dk)
			bysort caseid: gen reliable2_DK_w_dk=0.1331*(reliable2_DK-reliable_sum_w_dk)
			bysort caseid: gen reliable3_DK_w_dk=0.0927*(reliable3_DK-reliable_sum_w_dk)
			bysort caseid: gen reliable4_DK_w_dk=0.0830*(reliable4_DK-reliable_sum_w_dk)
			bysort caseid: gen reliable5_DK_w_dk=0.0812*(reliable5_DK-reliable_sum_w_dk)
			bysort caseid: gen reliable6_DK_w_dk=0.0788*(reliable6_DK-reliable_sum_w_dk)
			bysort caseid: gen reliable7_DK_w_dk=0.0551*(reliable7_DK-reliable_sum_w_dk)
			bysort caseid: gen reliable8_DK_w_dk=0.0514*(reliable8_DK-reliable_sum_w_dk)
			bysort caseid: gen reliable9_DK_w_dk=0.0379*(reliable9_DK-reliable_sum_w_dk)
			bysort caseid: gen reliable10_DK_w_dk=0.0264*(reliable10_DK-reliable_sum_w_dk)
			bysort caseid: gen reliable11_DK_w_dk=0.0333*(reliable11_DK-reliable_sum_w_dk)
			bysort caseid: gen reliable12_DK_w_dk=0.0367*(reliable12_DK-reliable_sum_w_dk)
		
		
			bysort caseid: gen reliable1_DK_w_dk_sq=reliable1_DK_w_dk^2
			bysort caseid: gen reliable2_DK_w_dk_sq=reliable2_DK_w_dk^2
			bysort caseid: gen reliable3_DK_w_dk_sq=reliable3_DK_w_dk^2
			bysort caseid: gen reliable4_DK_w_dk_sq=reliable4_DK_w_dk^2
			bysort caseid: gen reliable5_DK_w_dk_sq=reliable5_DK_w_dk^2
			bysort caseid: gen reliable6_DK_w_dk_sq=reliable6_DK_w_dk^2
			bysort caseid: gen reliable7_DK_w_dk_sq=reliable7_DK_w_dk^2
			bysort caseid: gen reliable8_DK_w_dk_sq=reliable8_DK_w_dk^2
			bysort caseid: gen reliable9_DK_w_dk_sq=reliable9_DK_w_dk^2
			bysort caseid: gen reliable10_DK_w_dk_sq=reliable10_DK_w_dk^2
			bysort caseid: gen reliable11_DK_w_dk_sq=reliable12_DK_w_dk^2
			bysort caseid: gen reliable12_DK_w_dk_sq=reliable12_DK_w_dk^2
		
			
	*then sum it all together  
									
			egen reliable_tot_sum=rowtotal(reliable1_DK_w_dk_sq-reliable12_DK_w_dk_sq), missing
			
	*lastly take the square root as Harteveld does
			
			bysort caseid: gen reliable_sqrt=sqrt(reliable_tot_sum)									
											
	*normalized variable
	gen reliable_w_n_dk=(reliable_sqrt-0.0055979)/(1.051985-0.0055979 )
	
	
	
	*****selfish
			bysort caseid: gen selfish1_DK_w=(selfish1_DK*0.2750)
			bysort caseid: gen selfish2_DK_w=(selfish2_DK*0.1331)
			bysort caseid: gen selfish3_DK_w=(selfish3_DK*0.0927)
			bysort caseid: gen selfish4_DK_w=(selfish4_DK*0.0830) 
			bysort caseid: gen selfish5_DK_w=(selfish5_DK*0.0812)
			bysort caseid: gen selfish6_DK_w=(selfish6_DK*0.0788)
			bysort caseid: gen selfish7_DK_w=(selfish7_DK*0.0551)
			bysort caseid: gen selfish8_DK_w=(selfish8_DK*0.0514) 
			bysort caseid: gen selfish9_DK_w=(selfish9_DK*0.0379)
			bysort caseid: gen selfish10_DK_w=(selfish10_DK*0.0264)
			bysort caseid: gen selfish11_DK_w=(selfish11_DK*0.0333)
			bysort caseid: gen selfish12_DK_w=(selfish12_DK*0.0367) 
											
	**then sum all of sympathy_p1_v sympathy_p2_v for each year cluster to get the average like-i etc...
			egen selfish_sum_w_dk=rowtotal(selfish1_DK_w selfish2_DK_w selfish3_DK_w selfish4_DK_w selfish5_DK_w selfish6_DK_w selfish7_DK_w selfish8_DK_w selfish9_DK_w selfish10_DK_w selfish11_DK_w selfish12_DK_w), missing
			
			
	*now i want to subtract the weighted mean of sympathies from the sympathy for each individual party.
	*then square it 
			bysort caseid: gen selfish1_DK_w_dk=0.2750*(selfish1_DK-selfish_sum_w_dk)
			bysort caseid: gen selfish2_DK_w_dk=0.1331*(selfish2_DK-selfish_sum_w_dk)
			bysort caseid: gen selfish3_DK_w_dk=0.0927*(selfish3_DK-selfish_sum_w_dk)
			bysort caseid: gen selfish4_DK_w_dk=0.0830*(selfish4_DK-selfish_sum_w_dk)
			bysort caseid: gen selfish5_DK_w_dk=0.0812*(selfish5_DK-selfish_sum_w_dk)
			bysort caseid: gen selfish6_DK_w_dk=0.0788*(selfish6_DK-selfish_sum_w_dk)
			bysort caseid: gen selfish7_DK_w_dk=0.0551*(selfish7_DK-selfish_sum_w_dk)
			bysort caseid: gen selfish8_DK_w_dk=0.0514*(selfish8_DK-selfish_sum_w_dk)
			bysort caseid: gen selfish9_DK_w_dk=0.0379*(selfish9_DK-selfish_sum_w_dk)
			bysort caseid: gen selfish10_DK_w_dk=0.0264*(selfish10_DK-selfish_sum_w_dk)
			bysort caseid: gen selfish11_DK_w_dk=0.0333*(selfish11_DK-selfish_sum_w_dk)
			bysort caseid: gen selfish12_DK_w_dk=0.0367*(selfish12_DK-selfish_sum_w_dk)
		
		
			bysort caseid: gen selfish1_DK_w_dk_sq=selfish1_DK_w_dk^2
			bysort caseid: gen selfish2_DK_w_dk_sq=selfish2_DK_w_dk^2
			bysort caseid: gen selfish3_DK_w_dk_sq=selfish3_DK_w_dk^2
			bysort caseid: gen selfish4_DK_w_dk_sq=selfish4_DK_w_dk^2
			bysort caseid: gen selfish5_DK_w_dk_sq=selfish5_DK_w_dk^2
			bysort caseid: gen selfish6_DK_w_dk_sq=selfish6_DK_w_dk^2
			bysort caseid: gen selfish7_DK_w_dk_sq=selfish7_DK_w_dk^2
			bysort caseid: gen selfish8_DK_w_dk_sq=selfish8_DK_w_dk^2
			bysort caseid: gen selfish9_DK_w_dk_sq=selfish9_DK_w_dk^2
			bysort caseid: gen selfish10_DK_w_dk_sq=selfish10_DK_w_dk^2
			bysort caseid: gen selfish11_DK_w_dk_sq=selfish12_DK_w_dk^2
			bysort caseid: gen selfish12_DK_w_dk_sq=selfish12_DK_w_dk^2
		
			
	*then sum it all together  
									
			egen selfish_tot_sum=rowtotal(selfish1_DK_w_dk_sq-selfish12_DK_w_dk_sq), missing
			
	*lastly take the square root as Harteveld does
			
		bysort caseid: gen selfish_sqrt=sqrt(selfish_tot_sum)									
											
	*normalized variable
	gen selfish_w_n_dk=(selfish_sqrt-0.0046892)/(1.016492-0.0046892 )
	
	
	*****mean
			bysort caseid: gen mean1_DK_w=(mean1_DK*0.2750)
			bysort caseid: gen mean2_DK_w=(mean2_DK*0.1331)
			bysort caseid: gen mean3_DK_w=(mean3_DK*0.0927)
			bysort caseid: gen mean4_DK_w=(mean4_DK*0.0830) 
			bysort caseid: gen mean5_DK_w=(mean5_DK*0.0812)
			bysort caseid: gen mean6_DK_w=(mean6_DK*0.0788)
			bysort caseid: gen mean7_DK_w=(mean7_DK*0.0551)
			bysort caseid: gen mean8_DK_w=(mean8_DK*0.0514) 
			bysort caseid: gen mean9_DK_w=(mean9_DK*0.0379)
			bysort caseid: gen mean10_DK_w=(mean10_DK*0.0264)
			bysort caseid: gen mean11_DK_w=(mean11_DK*0.0333)
			bysort caseid: gen mean12_DK_w=(mean12_DK*0.0367) 
											
	**then sum all of sympathy_p1_v sympathy_p2_v for each year cluster to get the average like-i etc...
			egen mean_sum_w_dk=rowtotal(mean1_DK_w mean2_DK_w mean3_DK_w mean4_DK_w mean5_DK_w mean6_DK_w mean7_DK_w mean8_DK_w mean9_DK_w mean10_DK_w mean11_DK_w mean12_DK_w), missing
			
			
	*now i want to subtract the weighted mean of sympathies from the sympathy for each individual party.
	*then square it 
			bysort caseid: gen mean1_DK_w_dk=0.2750*(mean1_DK-mean_sum_w_dk)
			bysort caseid: gen mean2_DK_w_dk=0.1331*(mean2_DK-mean_sum_w_dk)
			bysort caseid: gen mean3_DK_w_dk=0.0927*(mean3_DK-mean_sum_w_dk)
			bysort caseid: gen mean4_DK_w_dk=0.0830*(mean4_DK-mean_sum_w_dk)
			bysort caseid: gen mean5_DK_w_dk=0.0812*(mean5_DK-mean_sum_w_dk)
			bysort caseid: gen mean6_DK_w_dk=0.0788*(mean6_DK-mean_sum_w_dk)
			bysort caseid: gen mean7_DK_w_dk=0.0551*(mean7_DK-mean_sum_w_dk)
			bysort caseid: gen mean8_DK_w_dk=0.0514*(mean8_DK-mean_sum_w_dk)
			bysort caseid: gen mean9_DK_w_dk=0.0379*(mean9_DK-mean_sum_w_dk)
			bysort caseid: gen mean10_DK_w_dk=0.0264*(mean10_DK-mean_sum_w_dk)
			bysort caseid: gen mean11_DK_w_dk=0.0333*(mean11_DK-mean_sum_w_dk)
			bysort caseid: gen mean12_DK_w_dk=0.0367*(mean12_DK-mean_sum_w_dk)
		
		
			bysort caseid: gen mean1_DK_w_dk_sq=mean1_DK_w_dk^2
			bysort caseid: gen mean2_DK_w_dk_sq=mean2_DK_w_dk^2
			bysort caseid: gen mean3_DK_w_dk_sq=mean3_DK_w_dk^2
			bysort caseid: gen mean4_DK_w_dk_sq=mean4_DK_w_dk^2
			bysort caseid: gen mean5_DK_w_dk_sq=mean5_DK_w_dk^2
			bysort caseid: gen mean6_DK_w_dk_sq=mean6_DK_w_dk^2
			bysort caseid: gen mean7_DK_w_dk_sq=mean7_DK_w_dk^2
			bysort caseid: gen mean8_DK_w_dk_sq=mean8_DK_w_dk^2
			bysort caseid: gen mean9_DK_w_dk_sq=mean9_DK_w_dk^2
			bysort caseid: gen mean10_DK_w_dk_sq=mean10_DK_w_dk^2
			bysort caseid: gen mean11_DK_w_dk_sq=mean12_DK_w_dk^2
			bysort caseid: gen mean12_DK_w_dk_sq=mean12_DK_w_dk^2
			
			
	*then sum it all together  
									
			egen mean_tot_sum=rowtotal(mean1_DK_w_dk_sq-mean12_DK_w_dk_sq), missing
			
	*lastly take the square root as Harteveld does
			
		bysort caseid: gen mean_sqrt=sqrt(mean_tot_sum)									
											
	*normalized variable
	gen mean_w_n_dk=(mean_sqrt-0.0046892 )/( 1.03219-0.0046892 )
	

	
*TRAITS INDEX WEIGHTED
factor intelligent_w_n_dk reliable_w_n_dk selfish_w_n_dk mean_w_n_dk
rotate
*two factors (int+reli, and self+mean)
alpha  intelligent_w_n_dk reliable_w_n_dk selfish_w_n_dk mean_w_n_dk, item
*0.76 in full sample
egen traits_dk_w=rowmean(intelligent_w_n_dk reliable_w_n_dk selfish_w_n_dk mean_w_n_dk)

*COMBINE SE AND DK
egen traits_n_w=rowtotal(traits_se_w traits_dk_w), missing


***********************************************************************************

*UNWEIGHTED MEAN DISTANCE FROM MOST LIKED PARTY

*the value for the most liked party in any of the waves
egen intelligent_max_dk=rowmax(intelligent1_DK-intelligent12_DK)

egen reliable_max_dk=rowmax(reliable1_DK-reliable12_DK)

egen selfish_max_dk=rowmax(selfish1_DK-selfish12_DK)

egen mean_max_dk=rowmax(mean1_DK-mean12_DK)

*the mean of this
egen traits_max_mean_dk=rowmean(intelligent_max_dk reliable_max_dk selfish_max_dk mean_max_dk)

*like_ip-like_max^2
	*take the difference between each party feeling from the mean
foreach x of varlist intelligent1_DK-intelligent12_DK {
bysort caseid: gen `x'_diff_max=(`x'-intelligent_max_dk)^2
}

*takes the difference between each party feeling from the mean
foreach x of varlist reliable1_DK-reliable12_DK {
bysort caseid: gen `x'_diff_max=(`x'-reliable_max_dk)^2
}

foreach x of varlist selfish1_DK-selfish12_DK {
bysort caseid: gen `x'_diff_max=(`x'-selfish_max_dk)^2
}

foreach x of varlist mean1_DK-mean12_DK {
bysort caseid: gen `x'_diff_max=(`x'-mean_max_dk)^2
}

*sum it up
egen traits_max_sum_dk=rowtotal(intelligent1_DK_diff_max-intelligent12_DK_diff_max reliable1_DK_diff_max-reliable12_DK_diff_max  selfish1_DK_diff_max-selfish12_DK_diff_max mean1_DK_diff_max-mean12_DK_diff_max)

*divide by number of parties and take the square root
	by caseid: gen traits_max_tot_dk=sqrt(traits_max_sum_dk/12)
													
	gen traits_dist_n_dk=(traits_max_tot_dk-0)/ (9.464848-0)
	*remove all Swedes
	replace traits_dist_n_dk=. if id_se!=.
	
	*combine Sweden and Denmark
	egen traits_dist_n=rowtotal(traits_dist_n_se traits_dist_n_dk)
	
	
******************************************************

*WEIGHTED MEAN DISTANCE FROM MOST LIKED PARTY

*multiply the vote share of each party by (like_ip-like_p_mean_max)^2 
			bysort caseid: gen intelligent1_max_w_dk=0.2750*(intelligent1_DK-traits_max_mean_dk)
			bysort caseid: gen intelligent2_max_w_dk=0.1331*(intelligent2_DK-traits_max_mean_dk)
			bysort caseid: gen intelligent3_max_w_dk=0.0927*(intelligent3_DK-traits_max_mean_dk)
			bysort caseid: gen intelligent4_max_w_dk=0.0830*(intelligent4_DK-traits_max_mean_dk)
			bysort caseid: gen intelligent5_max_w_dk=0.0812*(intelligent5_DK-traits_max_mean_dk)
			bysort caseid: gen intelligent6_max_w_dk=0.0788*(intelligent6_DK-traits_max_mean_dk)
			bysort caseid: gen intelligent7_max_w_dk=0.0551*(intelligent7_DK-traits_max_mean_dk)
			bysort caseid: gen intelligent8_max_w_dk=0.0514*(intelligent8_DK-traits_max_mean_dk)
			bysort caseid: gen intelligent9_max_w_dk=0.0379*(intelligent9_DK-traits_max_mean_dk)
			bysort caseid: gen intelligent10_max_w_dk=0.0264*(intelligent10_DK-traits_max_mean_dk)
			bysort caseid: gen intelligent11_max_w_dk=0.0333*(intelligent11_DK-traits_max_mean_dk)
			bysort caseid: gen intelligent12_max_w_dk=0.0367*(intelligent12_DK-traits_max_mean_dk)
			
			bysort caseid: gen intelligent1_max_w_sq=intelligent1_max_w_dk^2
			bysort caseid: gen intelligent2_max_w_sq=intelligent2_max_w_dk^2
			bysort caseid: gen intelligent3_max_w_sq=intelligent3_max_w_dk^2
			bysort caseid: gen intelligent4_max_w_sq=intelligent4_max_w_dk^2
			bysort caseid: gen intelligent5_max_w_sq=intelligent5_max_w_dk^2
			bysort caseid: gen intelligent6_max_w_sq=intelligent6_max_w_dk^2
			bysort caseid: gen intelligent7_max_w_sq=intelligent7_max_w_dk^2
			bysort caseid: gen intelligent8_max_w_sq=intelligent8_max_w_dk^2
			bysort caseid: gen intelligent9_max_w_sq=intelligent9_max_w_dk^2
			bysort caseid: gen intelligent10_max_w_sq=intelligent10_max_w_dk^2
			bysort caseid: gen intelligent11_max_w_sq=intelligent11_max_w_dk^2
			bysort caseid: gen intelligent12_max_w_sq=intelligent12_max_w_dk^2

			bysort caseid: gen reliable1_max_w_dk=0.2750*(reliable1_DK-traits_max_mean_dk)
			bysort caseid: gen reliable2_max_w_dk=0.1331*(reliable2_DK-traits_max_mean_dk)
			bysort caseid: gen reliable3_max_w_dk=0.0927*(reliable3_DK-traits_max_mean_dk)
			bysort caseid: gen reliable4_max_w_dk=0.0830*(reliable4_DK-traits_max_mean_dk)
			bysort caseid: gen reliable5_max_w_dk=0.0812*(reliable5_DK-traits_max_mean_dk)
			bysort caseid: gen reliable6_max_w_dk=0.0788*(reliable6_DK-traits_max_mean_dk)
			bysort caseid: gen reliable7_max_w_dk=0.0551*(reliable7_DK-traits_max_mean_dk)
			bysort caseid: gen reliable8_max_w_dk=0.0514*(reliable8_DK-traits_max_mean_dk)
			bysort caseid: gen reliable9_max_w_dk=0.0379*(reliable9_DK-traits_max_mean_dk)
			bysort caseid: gen reliable10_max_w_dk=0.0264*(reliable10_DK-traits_max_mean_dk)
			bysort caseid: gen reliable11_max_w_dk=0.0333*(reliable11_DK-traits_max_mean_dk)
			bysort caseid: gen reliable12_max_w_dk=0.0367*(reliable12_DK-traits_max_mean_dk)
			
			bysort caseid: gen reliable1_max_w_sq=reliable1_max_w_dk^2
			bysort caseid: gen reliable2_max_w_sq=reliable2_max_w_dk^2
			bysort caseid: gen reliable3_max_w_sq=reliable3_max_w_dk^2
			bysort caseid: gen reliable4_max_w_sq=reliable4_max_w_dk^2
			bysort caseid: gen reliable5_max_w_sq=reliable5_max_w_dk^2
			bysort caseid: gen reliable6_max_w_sq=reliable6_max_w_dk^2
			bysort caseid: gen reliable7_max_w_sq=reliable7_max_w_dk^2
			bysort caseid: gen reliable8_max_w_sq=reliable8_max_w_dk^2
			bysort caseid: gen reliable9_max_w_sq=reliable9_max_w_dk^2
			bysort caseid: gen reliable10_max_w_sq=reliable10_max_w_dk^2
			bysort caseid: gen reliable11_max_w_sq=reliable11_max_w_dk^2
			bysort caseid: gen reliable12_max_w_sq=reliable12_max_w_dk^2
			
			
			bysort caseid: gen selfish1_max_w_dk=0.2750*(selfish1_DK-traits_max_mean_dk)
			bysort caseid: gen selfish2_max_w_dk=0.1331*(selfish2_DK-traits_max_mean_dk)
			bysort caseid: gen selfish3_max_w_dk=0.0927*(selfish3_DK-traits_max_mean_dk)
			bysort caseid: gen selfish4_max_w_dk=0.0830*(selfish4_DK-traits_max_mean_dk)
			bysort caseid: gen selfish5_max_w_dk=0.0812*(selfish5_DK-traits_max_mean_dk)
			bysort caseid: gen selfish6_max_w_dk=0.0788*(selfish6_DK-traits_max_mean_dk)
			bysort caseid: gen selfish7_max_w_dk=0.0551*(selfish7_DK-traits_max_mean_dk)
			bysort caseid: gen selfish8_max_w_dk=0.0514*(selfish8_DK-traits_max_mean_dk)
			bysort caseid: gen selfish9_max_w_dk=0.0379*(selfish9_DK-traits_max_mean_dk)
			bysort caseid: gen selfish10_max_w_dk=0.0264*(selfish10_DK-traits_max_mean_dk)
			bysort caseid: gen selfish11_max_w_dk=0.0333*(selfish11_DK-traits_max_mean_dk)
			bysort caseid: gen selfish12_max_w_dk=0.0367*(selfish12_DK-traits_max_mean_dk)
			
			bysort caseid: gen selfish1_max_w_sq=selfish1_max_w_dk^2
			bysort caseid: gen selfish2_max_w_sq=selfish2_max_w_dk^2
			bysort caseid: gen selfish3_max_w_sq=selfish3_max_w_dk^2
			bysort caseid: gen selfish4_max_w_sq=selfish4_max_w_dk^2
			bysort caseid: gen selfish5_max_w_sq=selfish5_max_w_dk^2
			bysort caseid: gen selfish6_max_w_sq=selfish6_max_w_dk^2
			bysort caseid: gen selfish7_max_w_sq=selfish7_max_w_dk^2
			bysort caseid: gen selfish8_max_w_sq=selfish8_max_w_dk^2
			bysort caseid: gen selfish9_max_w_sq=selfish9_max_w_dk^2
			bysort caseid: gen selfish10_max_w_sq=selfish10_max_w_dk^2
			bysort caseid: gen selfish11_max_w_sq=selfish11_max_w_dk^2
			bysort caseid: gen selfish12_max_w_sq=selfish12_max_w_dk^2
			
			
			bysort caseid: gen mean1_max_w_dk=0.2750*(mean1_DK-traits_max_mean_dk)
			bysort caseid: gen mean2_max_w_dk=0.1331*(mean2_DK-traits_max_mean_dk)
			bysort caseid: gen mean3_max_w_dk=0.0927*(mean3_DK-traits_max_mean_dk)
			bysort caseid: gen mean4_max_w_dk=0.0830*(mean4_DK-traits_max_mean_dk)
			bysort caseid: gen mean5_max_w_dk=0.0812*(mean5_DK-traits_max_mean_dk)
			bysort caseid: gen mean6_max_w_dk=0.0788*(mean6_DK-traits_max_mean_dk)
			bysort caseid: gen mean7_max_w_dk=0.0551*(mean7_DK-traits_max_mean_dk)
			bysort caseid: gen mean8_max_w_dk=0.0514*(mean8_DK-traits_max_mean_dk)
			bysort caseid: gen mean9_max_w_dk=0.0379*(mean9_DK-traits_max_mean_dk)
			bysort caseid: gen mean10_max_w_dk=0.0264*(mean10_DK-traits_max_mean_dk)
			bysort caseid: gen mean11_max_w_dk=0.0333*(mean11_DK-traits_max_mean_dk)
			bysort caseid: gen mean12_max_w_dk=0.0367*(mean12_DK-traits_max_mean_dk)
			
			bysort caseid: gen mean1_max_w_sq=mean1_max_w_dk^2
			bysort caseid: gen mean2_max_w_sq=mean2_max_w_dk^2
			bysort caseid: gen mean3_max_w_sq=mean3_max_w_dk^2
			bysort caseid: gen mean4_max_w_sq=mean4_max_w_dk^2
			bysort caseid: gen mean5_max_w_sq=mean5_max_w_dk^2
			bysort caseid: gen mean6_max_w_sq=mean6_max_w_dk^2
			bysort caseid: gen mean7_max_w_sq=mean7_max_w_dk^2
			bysort caseid: gen mean8_max_w_sq=mean8_max_w_dk^2
			bysort caseid: gen mean9_max_w_sq=mean9_max_w_dk^2
			bysort caseid: gen mean10_max_w_sq=mean10_max_w_dk^2
			bysort caseid: gen mean11_max_w_sq=mean11_max_w_dk^2
			bysort caseid: gen mean12_max_w_sq=mean12_max_w_dk^2
			
			*sum it up
			egen traits_sum_max_dk=rowtotal(intelligent1_max_w_sq-intelligent12_max_w_sq reliable1_max_w_sq-reliable12_max_w_sq selfish1_max_w_sq-selfish12_max_w_sq mean1_max_w_sq-mean12_max_w_sq) 
			
			by caseid: gen traits_max_v_dk=sqrt(traits_sum_max_dk)
	
			gen traits_dist_n_dk_w=(traits_max_v_dk-0)/ ( 3.485764-0)

			*Combine
			egen traits_dist_n_w=rowtotal(traits_dist_n_se_w traits_dist_n_dk_w)

